<?php


class CustomarOrderTable extends myDoctrineTable {

    public static function getInstance() {
        return Doctrine_Core::getTable('CustomarOrder');
    }
    public function addCustomerOrder($user = null, $shoppingCart = null, $remote_address = null) {
//        $userPaymentInfo = new UserPaymentInfo();
//        $userPaymentInfo->user_id = $user->getDmUser()->getId();
//        $userPaymentInfo->save();
        $customarOrderInfo = CustomarOrderTable::getInstance()->findAll();
        foreach ($customarOrderInfo as $value) {
            $orderid = $value->id;
        }
        
 
        if(!$orderid){
            $orderid = 989898;
        }
        $order = new CustomarOrder();
        $order->session_id = $user->getCartCookieSession();
        $order->remote_ip = $remote_address;
        $order->customer_id = $user->getDmUser()->getId();
        $order->id = $orderid - 1;
        $order->status = 'Processing';
        $order->total_cost = ($shoppingCart->getTotal());
        $order->save();
        foreach($shoppingCart->getItems() as $item) {
            $customarOrderProduct = new CustomarOrderProduct();
            $customarOrderProduct->product_id = $item->getId();
            $customarOrderProduct->customar_order_id = $order->getId();
            $customarOrderProduct->quantity = $item->getQuantity();
            $customarOrderProduct->unit_cost = $item->getPrice();
            $customarOrderProduct->size = $item->getSize();
            $customarOrderProduct->color = $item->getColor();
            $customarOrderProduct->save();
            if($customarOrderProduct) {
                $this->updateProductQty($item->getId(), $item->getQuantity());
            }
        }
        $this->updateGroup($user->getDmUser()->getId(),'Prime');
        return $order;
    }

    public function updateProductQty($product_id = null,$qty = 0) {
        $product = ProductTable::getInstance()->find($product_id);
        $product->setSellingQty(($product->getSellingQty()+$qty));
        $product->save();
    }
    public function updateGroup($user_id = null, $type = null) {
        $group = DmGroupTable::getInstance()->findOneByName($type);
        if(!$group) {
            $group = new DmGroup();
            $group->name = $type;
            $group->save();
        }

        $usergroup = DmUserGroupTable::getInstance()->findOneByDmUserId($user_id);
        if($usergroup) {

            if(($usergroup->dm_group_id) !=  $group->id && $type =='Premier' ) {
                $usergroup->delete();
                $usergroup = new DmUserGroup();
                $usergroup->dm_user_id = $user_id;
                $usergroup->dm_group_id = $group->id;
                $usergroup->save();
            }
        }
        else if(!$usergroup) {
            $usergroup = new DmUserGroup();
            $usergroup->dm_user_id = $user_id;
            $usergroup->dm_group_id = $group->id;
            $usergroup->save();
        }
//        die();
    }
}